package com.ainiyar.springweb.util;

import com.ainiyar.springweb.common.result.ResultResponse;
import com.alibaba.fastjson2.JSON;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**************************************
 * File Name : WebUtil
 * Created with : IntelliJ IDEA.
 * Author : 艾尼亚尔·毛拉吾提
 * QQ : 524155792  WeChat : ainiyar0124
 * Created Time : 2024/2/4  17:36
 * Desc :将字符串渲染到客户端
 ***************************************/
public class WebUtil {
    public static void renderString(HttpServletResponse response, ResultResponse resultResponse) throws IOException {
        String result = JSON.toJSONString(resultResponse);
        //获取输出流
        ServletOutputStream outputStream = response.getOutputStream();
        //将结果保存到输出中写出去
        outputStream.write(result.getBytes(StandardCharsets.UTF_8));
        outputStream.flush();
        outputStream.close();
    }
}

